<template>
  <!-- 血糖范围---就诊 ---唐莉 -->
  <el-dialog
    title="编辑血糖"
    :visible="value"
    width="700px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.native.prevent="onSubmit"
    >
      <el-row>
        <el-col v-for="(item,index) in itemList" :key="index" :span="24">
          <el-form-item label="记录时间">
            <el-date-picker
              v-model="item.date"
              type="date"
              class="sugar_date"
              :disabled="true"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            ></el-date-picker>
            <el-input class="sugar_select" v-model="item.type" disabled></el-input>
            <el-input class="sugar_input" v-model="item.value">
              <template slot="append">mmol/L</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem","patientId"],
  data() {
    return {
      form: {
        date: "",
        patientId:'',
        id:''
      },
      suagrId:0,
      itemList:[],
      rules: {},
      btnLoading: false
    };
  },
  created() {
    let th = this;
    th.form.date = th.getNewDate();
    th.onOpen();
  },
  methods: {
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    },
    onOpen() {
        let th=this;
        let list;
        if(th.curItem){
           list=[{
               type:'凌晨',
               id:1,
               value:th.curItem.weeHours,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'空腹',
               id:2,
               value:th.curItem.limosis,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'早餐前',
               id:3,
               value:th.curItem.beforeBreakfast,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'早餐后',
               id:4,
               value:th.curItem.afterBreakfast,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'午餐前',
               id:5,
               value:th.curItem.antePrandium,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'午餐后',
               id:6,
               value:th.curItem.afterLunch,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'晚餐前',
               id:7,
               value:th.curItem.beforeDinner,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'晚餐后',
               id:8,
               value:th.curItem.afterDinner,
               date:th.curItem.gmtCreate.substring(0,10)
           },{
               type:'睡前',
               id:9,
               value:th.curItem.beforeSleep,
               date:th.curItem.gmtCreate.substring(0,10)
           }]
           th.itemList=list;
           th.suagrId=th.curItem.id;
        }
        
    },
    onSubmit() {
      let th = this;
      let j = {
          weeHours:'',//凌晨
        limosis:'',//空腹
        beforeSleep:'',//睡前
        afterDinner:'',//晚餐后
        beforeDinner:'',//晚餐前
        afterLunch:'',//午餐后
        antePrandium:"",//午餐前
        afterBreakfast:'',//早餐后
        beforeBreakfast:'',//早餐前
      };
      th.btnLoading = true;
      th.form.patientId = th.patientId;
      th.form.id=th.suagrId;
      let list=th.itemList;
      for(let i=0;i<list.length;i++){
          if(list[i].id==1){
            j.weeHours=list[i].value;
          }
          if(list[i].id==2){
            j.limosis=list[i].value;
          }
          if(list[i].id==3){
            j.beforeBreakfast=list[i].value;
          }
          if(list[i].id==4){
            j.afterBreakfast=list[i].value;
          }
          if(list[i].id==5){
            j.antePrandium=list[i].value;
          }
          if(list[i].id==6){
            j.afterLunch=list[i].value;
          }
          if(list[i].id==7){
            j.beforeDinner=list[i].value;
          }
          if(list[i].id==8){
            j.afterDinner=list[i].value;
          }
          if(list[i].id==9){
            j.beforeSleep=list[i].value;
          }
      }
      Object.assign(j, th.form);
      th.$api.savePatientGlucoseRecord(j).then(res => {
          if (res.executed) {
            th.$message.success("保存成功");
            th.$emit("success");
            th.onClose();
            th.btnLoading = false;
          } else {
            th.$message.error(res.message);
            // th.onClose();
            th.btnLoading = false;
          }
        })
        .catch(error => {
          th.btnLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.btn_sign_option {
  padding: 10px;
}
.sugar_date{
    width:30%!important;
    margin-right: 2%;
}
.sugar_select{
    width:15%;
    margin-right: 2%;
}
.sugar_input {
  width: 40%;
}
</style>